Skip to content

Conversation

@rragundez
Copy link
Contributor

@rragundez rragundez commented Nov 18, 2025

There are several potential bugs on the current way of loading of settings via environment variables and .env files, mainly caused by a misconfiguration where both, the pydantic way and the starlette way of loading the settings are being used.
This can create quite obfuscated bugs as each one has its own logic and loading time (at import and/or at instantiation). I think the reason why it has not presented itself in a big way in the current form of the code is because all settings have a primitive type (even though it fails now for composite settings where the setting retains the default values and not the ones in the .env file e.g. the databases uris).

It's quite cumbersome to explain every scenario that breaks but in a nutshell there is a lack of consistency. The starlette way loads at import time, it does define the path to the .env file but it prioritizes loading from environment variables. In addition, the pydantic way loads both at import time and instantiation time, at instantiation time it does it from environment variables and it will override any values set by the starlette way at import time. To top it up, the docker compose definition adds the .env file both as environment variables and as an .env file that starlette loads into config.

This is the first PR of several that will try to correct and simplify how settings are being loaded from environment variables or .env files, and to simplify the documentation. Also at the moment it seems the repository promotes the idea of making an .env file part of the src code and using an .env file in staging or production, which both are not a good practice.

In particular this PR at the moment removes the loading of duplicated settings coming from starlette and pydantic, and applies only the pydantic best practice. This means that composite settings cannot be formed during import time as it is now but they have to be computed, if not they would pick up the default value only, regardless if an environment variable was set.
In addition, it starts aligning with the idea that regardless if coming from environment variables directly or from and .env file, settings will always be loaded from environment variables when running the application, therefore separating both tasks ( setting env variables and loading settings)

@rragundez
Copy link
Contributor Author

Just to clarify that the code is broken at the moment without these changes @igorbenav @carlosplanchon

@igorbenav
Copy link
Collaborator

@LucasQR

@igorbenav igorbenav requested a review from LucasQR November 19, 2025 17:39
…g the native pydantic casting of env variables to lists via JSON conversion
@LucasQR
Copy link
Collaborator

LucasQR commented Nov 19, 2025

Hey @rragundez , I reviewed this one, and before I can approve and merge it, I'd like to know if this and the other two prs are finished. I intend to review ll three before merging them

@LucasQR LucasQR merged commit fd11216 into benavlabs:main Nov 19, 2025
3 checks passed
@rragundez
Copy link
Contributor Author

Hi @LucasQR , thanks so much for reviewing it :)
I made smaller PRs so they can be reviewed easier, but they should be atomic, of course if you prefer to review a big one with everything in it let me know and I can take that approach instead.

Good point about if the other PRs are finished, the WIP might be confusing, I will make sure to be clear if it is finished or not.

@rragundez rragundez deleted the fix-settings branch November 20, 2025 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants